package com.cy.shiro.controller;

import com.cy.shiro.pojo.SysUserRole;
import com.cy.shiro.service.SysUserRoleService;
import com.cy.shiro.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService sysUserRoleService;


    //修改权限
    @PostMapping("/userRoles/roleEditPost")
    @ResponseBody
    @CrossOrigin
    public Object roleEditPost(HttpServletRequest request){

        String roleId = request.getParameter("roleId");
        String userId = request.getParameter("userId");
        Integer resultId = 0;

        Map<String,String> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("userId", userId);

        SysUserRole sysUserRole = sysUserRoleService.find(Integer.valueOf(userId));
        //不存在则新增否则修改
        if(sysUserRole == null){
             resultId = sysUserRoleService.addPost(map);
        } else{
             resultId = sysUserRoleService.editPost(map);
        }

        if("0".equals(String.valueOf(resultId)) || "null".equals(String.valueOf(resultId))){
            return ResultUtil.error("权限修改失败");
        }

        return ResultUtil.success("权限修改成功");
    }
}
